package com.maoye.ossp.biz.brand.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.hotent.base.core.util.string.StringUtil;
import com.maoye.ossp.biz.brand.entity.BrandPositionLog;
import com.maoye.ossp.biz.brand.service.BrandPositionLogService;
import com.maoye.ossp.biz.brand.service.IBrandPicService;
import com.maoye.ossp.biz.brand.utils.BrandCommonConstant;
import com.maoye.ossp.biz.contract.utils.ContractUtils;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.SysBaseFloorAttment;
import com.maoye.ossp.sys.base.service.SysBaseFloorAttmentService;
import com.maoye.ossp.sys.org.util.UserUtils;
import com.maoye.sc.api.domain.User;
import com.maoye.sc.service.user.UserServiceImpl;

/**
 * 品牌落位发送查询
* @ClassName: BrandPositionController
* @Description: 
* @Company: MOI
* @author linzhizhong
* @date 2014年6月3日 下午10:27:29
*
 */
@Controller
@RequestMapping("biz/brand/brandPositionLog/")
public class BrandPositionLogController extends BaseController {
	
	@Resource
	BrandPositionLogService brandPositionLogService;
	
	
	@Resource
	SysBaseFloorAttmentService sysBaseFloorAttmentService;    //楼层附件

	
	
	/**
	 * 供应商查询品牌落发布信息
	 * @param brandLevel
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	
	@Resource
	private UserServiceImpl  userServiceImpl;
	
	
	@RequestMapping(value = "supplierList")
	public String supplierList(BrandPositionLog brandPositionLog, HttpServletRequest request, HttpServletResponse response, Model model) {

			QueryFilter queryFilter = getQuerFilter(request, brandPositionLog);
			if(StringUtil.isNotEmpty(brandPositionLog.getFloorId())){
				queryFilter.addFilter("floorId", brandPositionLog.getFloorId(), QueryOP.EQUAL);
			}
			if(StringUtil.isNotEmpty(brandPositionLog.getStoreId())){
				queryFilter.addFilter("storeId", brandPositionLog.getStoreId(), QueryOP.EQUAL);
			}
			if(StringUtil.isNotEmpty(brandPositionLog.getTitle())){
				queryFilter.addFilter("title", brandPositionLog.getTitle(), QueryOP.LIKE);
			}
			User user=userServiceImpl.getUserById(UserUtils.getCurrentUserId().toString());
			
			if(ContractUtils.USER_TYPE_S.equals(user.getUserType())){
				queryFilter.addFilter("supplierId", user.getId().toString(), QueryOP.EQUAL);
			}else{
				queryFilter.addFilter("supplierId", user.getId().toString(), QueryOP.EQUAL);
			}
			
			if(StringUtil.isNotEmpty(brandPositionLog.getStatus())){
				queryFilter.addFilter("status", brandPositionLog.getStatus(), QueryOP.EQUAL);
			}else{
				queryFilter.addFilter("status","00", QueryOP.EQUAL);	     //默已发起合作的
			}	
		
			if(brandPositionLog.getBrandCate()!=null&&StringUtil.isNotEmpty(brandPositionLog.getBrandCate().getBrandCname())){
				queryFilter.addFilter("brandCname", brandPositionLog.getBrandCate().getBrandCname(), QueryOP.LIKE);
			}
			if(brandPositionLog.getBrandCate()!=null&&StringUtil.isNotEmpty(brandPositionLog.getBrandCate().getBrandNo())){
				queryFilter.addFilter("brandNo", brandPositionLog.getBrandCate().getBrandNo(), QueryOP.LIKE);
			}
		
			Page<BrandPositionLog> page = brandPositionLogService.find(queryFilter);
		
			model.addAttribute("page", page);
			model.addAttribute("brandPositionLog", brandPositionLog);
			return resultPage("/biz/brand/resetBrand/supplierList", model, request);
	}
	
	
	
	/**
	 * 采购查询已合作招商发布信息
	* @Title: purList
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param brandPositionLog
	* @param @param request
	* @param @param response
	* @param @param model
	* @param @return    设定文件
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value = "purList")
	public String purList(BrandPositionLog brandPositionLog, HttpServletRequest request, HttpServletResponse response, Model model) {

			QueryFilter queryFilter = getQuerFilter(request, brandPositionLog);
			if(StringUtil.isNotEmpty(brandPositionLog.getFloorId())){
				queryFilter.addFilter("floorId", brandPositionLog.getFloorId(), QueryOP.EQUAL);
			}
			if(StringUtil.isNotEmpty(brandPositionLog.getStoreId())){
				queryFilter.addFilter("storeId", brandPositionLog.getStoreId(), QueryOP.EQUAL);
			}
			if(StringUtil.isNotEmpty(brandPositionLog.getTitle())){
				queryFilter.addFilter("title", brandPositionLog.getTitle(), QueryOP.LIKE);
			}
			if(StringUtil.isNotEmpty(brandPositionLog.getTitle())){
				queryFilter.addFilter("title", brandPositionLog.getTitle(), QueryOP.LIKE);
			}	
			if(StringUtil.isNotEmpty(brandPositionLog.getStatus())){
				queryFilter.addFilter("status", brandPositionLog.getStatus(), QueryOP.EQUAL);
			}else{
				queryFilter.addFilter("status","10", QueryOP.EQUAL);	     //默已发起合作的
			}	
		
			if(brandPositionLog.getBrandCate()!=null&&StringUtil.isNotEmpty(brandPositionLog.getBrandCate().getBrandCname())){
				queryFilter.addFilter("brandCname", brandPositionLog.getBrandCate().getBrandCname(), QueryOP.LIKE);
			}
			if(brandPositionLog.getBrandCate()!=null&&StringUtil.isNotEmpty(brandPositionLog.getBrandCate().getBrandNo())){
				queryFilter.addFilter("brandNo", brandPositionLog.getBrandCate().getBrandNo(), QueryOP.LIKE);
			}
			
			
			if(brandPositionLog.getSupplier()!=null&&StringUtil.isNotEmpty(brandPositionLog.getSupplier().getCname())){
				queryFilter.addFilter("cname", brandPositionLog.getSupplier().getCname(), QueryOP.LIKE);
			}
			if(brandPositionLog.getSupplier()!=null&&StringUtil.isNotEmpty(brandPositionLog.getSupplier().getSupplierNo())){
				queryFilter.addFilter("supplierNo", brandPositionLog.getSupplier().getSupplierNo(), QueryOP.LIKE);
			}
			Page<BrandPositionLog> page = brandPositionLogService.find2(queryFilter);
			model.addAttribute("page", page);
			model.addAttribute("brandPositionLog", brandPositionLog);
			return resultPage("/biz/brand/resetBrand/purList", model, request);
	}
	
	/**
	 * 供应商查看
	* @Title: spplierView
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param id
	* @param @param model
	* @param @return    设定文件
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value = "{id}/supplierView")
	public String supplierView(@PathVariable String id, Model model,HttpServletRequest request) 
	{
		BrandPositionLog brandPositionLog = brandPositionLogService.get(Long.valueOf(id));
		SysBaseFloorAttment floor = new SysBaseFloorAttment();
		QueryFilter queryFilter = getQuerFilter(request,floor);
		queryFilter.addFilter("floorNo", brandPositionLog.getBaseFloor().getCode(), QueryOP.EQUAL);
		Page<SysBaseFloorAttment> page = (Page<SysBaseFloorAttment>)sysBaseFloorAttmentService.find(queryFilter);
		List<SysBaseFloorAttment> attList=page.getList();
		
		User user=userServiceImpl.getUserById(UserUtils.getCurrentUserId().toString());
		model.addAttribute("brandPositionLog", brandPositionLog);
		model.addAttribute("userType", user.getUserType());
		if(attList!=null&&!attList.isEmpty()){
			model.addAttribute("floorAttId", attList.get(0).getId().toString());
		}
		return "/biz/brand/resetBrand/view";
	}
	
	/**
	 * 采购查看
	* @Title: spplierView
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param id
	* @param @param model
	* @param @return    设定文件
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable String id, Model model,HttpServletRequest request) 
	{
		String navId = request.getParameter("navId");
		BrandPositionLog brandPositionLog = brandPositionLogService.get(Long.valueOf(id));
		SysBaseFloorAttment floor = new SysBaseFloorAttment();
		QueryFilter queryFilter = getQuerFilter(request,floor);
		queryFilter.addFilter("floorNo", brandPositionLog.getBaseFloor().getCode(), QueryOP.EQUAL);
		Page<SysBaseFloorAttment> page = (Page<SysBaseFloorAttment>)sysBaseFloorAttmentService.find(queryFilter);
		List<SysBaseFloorAttment> attList=page.getList();
		
		User user=userServiceImpl.getUserById(UserUtils.getCurrentUserId().toString());
		model.addAttribute("brandPositionLog", brandPositionLog);
		model.addAttribute("userType", user.getUserType());
		model.addAttribute("navId", navId);
		if(attList!=null&&!attList.isEmpty()){
			model.addAttribute("floorAttId", attList.get(0).getId().toString());
		}
		return "/biz/brand/resetBrand/view";
	}
	
	/**
	 * @throws Exception 
	 * 供应商查看
	* @Title: spplierView
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param id
	* @param @param model
	* @param @return    设定文件
	* @return String    返回类型
	* @throws
	 */
	
	
	@RequestMapping(value = "{id}/supplierUpdate")
	public String supplierUpdate(@PathVariable String id, Model model,HttpServletRequest request) throws Exception 
	{
		BrandPositionLog brandPositionLog = brandPositionLogService.get(Long.valueOf(id));
		brandPositionLog.setStatus(BrandCommonConstant.BRANDPOSITION_SATUES_10);
		brandPositionLogService.update(brandPositionLog);
		model.addAttribute("brandPositionLog", brandPositionLog);
		User user=userServiceImpl.getUserById(UserUtils.getCurrentUserId().toString());
		model.addAttribute("userType", user.getUserType());
		brandPositionLogService.sendToDoMessage(brandPositionLog);
		return resultPage("/biz/brand/resetBrand/view",model,request);
	}
	
	
	@RequestMapping(value = "{publishIdid}/getHistList")
	public String getHistList(@PathVariable String publishIdid, Model model,HttpServletRequest request) 
	{
		BrandPositionLog brandPositionLog = new BrandPositionLog();
		QueryFilter queryFilter = getQuerFilter(request, brandPositionLog);
		queryFilter.addFilter("publishId", publishIdid, QueryOP.EQUAL);
		
		Page<BrandPositionLog> page = brandPositionLogService.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("brandPositionLog", brandPositionLog);
		return resultPage("/biz/brand/resetBrand/histList", model, request);
	}

	/**
	 * @throws Exception 
	 * 批量提交 SAP
	* @Title: removeBatch
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param ids
	* @param @param request
	* @param @param model
	* @param @param redirectAttributes
	* @param @return    设定文件
	* @return String    返回类型
	* @throws
	 */
	@RequestMapping(value = "{ids}/submitBrand")
	public String submitBrand(@PathVariable String ids, HttpServletRequest request,Model model,RedirectAttributes redirectAttributes) throws Exception {
		String navId = request.getParameter("navId");
		String[] idarry =ids.split(",");
		for (int i = 0; i < idarry.length; i++) {
			BrandPositionLog brandPositionLog = brandPositionLogService.get(Long.valueOf(idarry[i]));
			if(brandPositionLog.getStatus().equals(BrandCommonConstant.BRANDPOSITION_SATUES_00)){
				brandPositionLog.setStatus(BrandCommonConstant.BRANDPOSITION_SATUES_10);
				brandPositionLogService.update(brandPositionLog);
				brandPositionLogService.sendToDoMessage(brandPositionLog);
			}
		}
		return "redirect:/biz/brand/brandPositionLog/supplierList?navId="+navId;
	}
	
	
	
	
}
